package ace.cmp.data.methodname.method.name.sql.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author caspar
 * @date 2023/2/18 9:39
 */
public final class StringUtils {
  public static final String EMPTY = "";

  /**
   * 首字母转小写
   *
   * @param s
   * @return
   */
  public static String toLowerCaseFirstOne(String s) {
    if (Character.isLowerCase(s.charAt(0))) return s;
    else
      return (new StringBuilder())
          .append(Character.toLowerCase(s.charAt(0)))
          .append(s.substring(1))
          .toString();
  }

  /**
   * 首字母转大写
   *
   * @param s
   * @return
   */
  public static String toUpperCaseFirstOne(String s) {
    if (Character.isUpperCase(s.charAt(0))) return s;
    else
      return (new StringBuilder())
          .append(Character.toUpperCase(s.charAt(0)))
          .append(s.substring(1))
          .toString();
  }

  public static boolean isEmpty(String s) {
    return s == null || s.length() == 0;
  }

  /***
   * 将驼峰转为下划线
   *
   **/
  public static String humpToUnderline(String str) {
    Pattern compile = Pattern.compile("[A-Z]");
    Matcher matcher = compile.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
    }
    matcher.appendTail(sb);
    return sb.toString();
  }

  /***
   * 将下划线转为驼峰
   *
   **/
  public static String underlineToHump(String str) {
    str = str.toLowerCase();
    Pattern compile = Pattern.compile("_[a-z]");
    Matcher matcher = compile.matcher(str);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      matcher.appendReplacement(sb, matcher.group(0).toUpperCase().replace("_", ""));
    }
    matcher.appendTail(sb);
    return sb.toString();
  }
}
